package com.blb.thread;

/**
 * 可见性案例
 */
public class VolatileDemo {

    public volatile boolean flag = true;

    public void testVolatile(){
        System.out.println("程序执行！");
        while(flag){ }
        System.out.println("程序结束！");
    }

    public static void main(String[] args) throws InterruptedException {
        VolatileDemo demo = new VolatileDemo();
        //新建线程执行方法
        new Thread(() -> {
            demo.testVolatile();
        }).start();
        Thread.sleep(3000L);
        //主线程修改flag变量
        demo.flag = false;
        System.out.println("主线程结束");
    }
}
